/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filmlibrary.web;

import filmlibrary.orm.ORMHelper;
import filmlibrary.orm.classes.Users;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author karol
 */
@ManagedBean
@RequestScoped
public class PassBean  implements java.io.Serializable{
    private static final Logger logger = Logger.getLogger("filmlibrary.web.PassBean");
    private ORMHelper orm = new ORMHelper();
    private String oldPass="";
    private String newPass="";
    private String newPass2="";
    private String message=null;
    private String message1=null;
    /** Creates a new instance of PassBean */
    public PassBean() {
    }
    public void validatePassLength(FacesContext facesContex, UIComponent uiComponent, Object value) throws ValidatorException{
        if(((String)value).length()<5){
            HtmlInputSecret htmlInputSecret = (HtmlInputSecret) uiComponent;
            FacesMessage facesMessage = new FacesMessage(
                    htmlInputSecret.getLabel()+": za krótkie hasło");
            throw new ValidatorException(facesMessage);
        }
    }

    public String changePass(){
        if(!newPass.equals(newPass2)){
            message="nowe hasła nie zgadzają się";
            return "pass.xhtml";
        }
        Users u=orm.getUserbyName(new SessionBean().getLogin());
        if(!Additional.MD5(oldPass).equals(u.getPassword())){
            message="Stare hasło jest nieprawdiłowe";
            return "pass.xhtml";
        }
        u.setPassword(Additional.MD5(newPass));
        orm.updateUser(u);
        message="hasło zmienione";
        return "pass.xhtml";
        
    }

    public String getNewPass() {
        return newPass;
    }

    public void setNewPass(String newPass) {
        this.newPass = newPass;
    }

    public String getNewPass2() {
        return newPass2;
    }

    public void setNewPass2(String newPass2) {
        this.newPass2 = newPass2;
    }

    public String getOldPass() {
        return oldPass;
    }

    public void setOldPass(String oldPass) {
        this.oldPass = oldPass;
    }

    public String getMessage() {
        return message;
    }

    

}
